//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj设置登录密码 mj修改密码

#import "ChangPasswordVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "RegisterViewController.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
#import "CaptchaView.h"
#import "EnterCodeController.h"
#import "MJConfirmLoadingView.h"
#import "AccountVerificationVC.h"

@interface ChangPasswordVC ()
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet MJConfirmLoadingView *confirmButton;
@property (weak, nonatomic) IBOutlet UIButton *encryptButton;
@property (weak, nonatomic) IBOutlet UIButton *resetEncryptButton;
@property (weak, nonatomic) IBOutlet MJUITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet MJUITextField *resetPasswordTextField;

@end

@implementation ChangPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    
    //
    [self.passwordTextField mj_becomeFirstResponder];
    [self.encryptButton setImage:MJUIImageMake(@"icon_biyan_p") forState:0];
    [self.encryptButton setImage:MJUIImageMake(@"icon_zhengyan_p") forState:UIControlStateSelected];
    
    [self.resetEncryptButton setImage:MJUIImageMake(@"icon_biyan_p") forState:0];
    [self.resetEncryptButton setImage:MJUIImageMake(@"icon_zhengyan_p") forState:UIControlStateSelected];
    [self.confirmButton.continueButton setTitle:MJUILocalizedString(@"Done") forState:0];
    self.passwordTextField.mj_limitedNumber = 16;
    self.resetPasswordTextField.mj_limitedNumber = 16;

    self.passwordTextField.secureTextEntry = true;
    self.resetPasswordTextField.secureTextEntry = true;

    //
    MJWeakSelf
    self.confirmButton.handleContinueBlock = ^{
        [weakSelf handleConfirmButtonClick];

    };

    RAC(self.confirmButton, enabled) = [RACSignal combineLatest:@[self.passwordTextField.rac_textSignal,self.resetPasswordTextField.rac_textSignal] reduce:^id _Nullable(NSString * text,NSString * newText){
        
        BOOL enable = false;
        if(text.length > 5){
            if(text.mj_containsUpperCaseLetters && text.mj_containsLowerCaseLetters && text.mj_containsDigits){
                enable = true;
            }
        }
        
        BOOL newEnable = false;
        if(newText.length > 5){
            if(newText.mj_containsUpperCaseLetters && newText.mj_containsLowerCaseLetters && newText.mj_containsDigits){
                newEnable = true;
            }
        }
        
        if(enable && newEnable){
            weakSelf.tipLabel.hidden = true;
            return @(true);

        }else{
            weakSelf.tipLabel.hidden = false;
            return @(false);

        }
        
    }];
}

#pragma mark - 方法
- (IBAction)handleForgotPasswordButtonClick:(UIButton *)sender {
    AccountVerificationVC *vc = [[AccountVerificationVC alloc]init];
    NSString *phoneNumber = UserManager.shared.userModel.phoneNumber;
    if(phoneNumber){
        vc.phoneNumber = phoneNumber;
    }else{
        vc.email = UserManager.shared.userModel.email;
    }
    [self.navigationController pushViewController:vc animated:true];
}
- (IBAction)handleRestEncryptButtonClick:(UIButton *)sender {
    [self.resetEncryptButton setSelected:!sender.isSelected];
    
    if(sender.isSelected){
        self.resetPasswordTextField.secureTextEntry = false;
    }else{
        self.resetPasswordTextField.secureTextEntry = true;
    }
}
- (IBAction)handleEncryptButtonClick:(UIButton *)sender {
    [self.encryptButton setSelected:!sender.isSelected];
    
    if(sender.isSelected){
        self.passwordTextField.secureTextEntry = false;
    }else{
        self.passwordTextField.secureTextEntry = true;
    }
}

- (void)requestData{
    
}


- (void)handleConfirmButtonClick{
    
    [self.passwordTextField resignFirstResponder];
    [self.resetPasswordTextField resignFirstResponder];

    if([self.passwordTextField.text isEqualToString:self.resetPasswordTextField.text]){
     
        [self.confirmButton.loadingView stopAnimation];

        [RoyHud showToast:MJUILocalizedString(@"The new password cannot be the same as the current one. Please create a different password.")];
        return;
    }
    
    [self.confirmButton.loadingView startAnimation];
//
    NSString *currentPassword = [HealthHelper aesEncryptString:self.passwordTextField.text];
    NSString *newPassword = [HealthHelper aesEncryptString:self.resetPasswordTextField.text];

    NSDictionary *params =  @{
        @"currentPassword": currentPassword,
        @"newPassword": newPassword,
    };
    MJWeakSelf
    [MJHttpNetWorking requestWithParams:params requestUri:user_changePassword requestType:RequestTypePut showLoading:false success:^(MJBaseModel *model) {
        [weakSelf.confirmButton.loadingView stopAnimation];

        if(model.code == 200){
            [RoyHud showToast:MJUILocalizedString(@"Setting successful")];
            [weakSelf.navigationController popViewControllerAnimated:true];

        }else if(model.code == 412){
            [RoyHud showToast:MJUILocalizedString(@"Incorrect current password. Please check and change then try again.")];

        }

    } failure:^(NSError *error) {
        [weakSelf.confirmButton.loadingView stopAnimation];

    }];
    

}


#pragma mark - 代理

#pragma mark - 懒加载

@end
